#include "StatsState.h"

StatsState* StatsState::statsState = 0;

StatsState::StatsState(sf::RenderWindow* window, sfge::StateManager* mgr) : State(window, mgr)
{

}

StatsState::~StatsState()
{
    //dtor
}

void StatsState::init()
{
    backButton = new Button("Back", 400, 500);
    mfont.LoadFromFile("Data/Fonts/gamecuben.TTF");

    initSprites();
    readStats();
}

void StatsState::cleanup()
{
    delete backButton;
}

void StatsState::initSprites()
{
    background.SetTexture(rm->getTexture("menu.png"));
    cursorSprite.SetTexture(rm->getTexture("cursor.png"));
    sf::Sprite sprite;
        sprite.SetTexture(rm->getTexture("Brick.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("explosiveBrick.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("greenBrick.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("minibrick.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("pinBrick.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("biggerPanelBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("smallerPanelBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("liveBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("pauseBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("acidBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("ballBonus.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("highscore.png"), true);
    sprites.push_back(sprite);
        sprite.SetTexture(rm->getTexture("timePlayed.png"), true);
    sprites.push_back(sprite);
}


void StatsState::readStats()
{
    std::ifstream file;
    std::string buffer;
    file.open("Data/stats.txt");
    std::string score;
    for(int i = 0; i < 5; i++)
    {
        file >> score;

        sf::Text scoreText(score);
        scoreText.SetFont(mfont);
        sprites[i].SetPosition(75, i*50 + 150);
        scoreText.SetPosition(175, i*50 + 150);
        scores.push_back(scoreText);
    }

    for(int i = 5; i < 11; i++)
    {
        file >> score;

        sf::Text scoreText(score);
        scoreText.SetFont(mfont);
        sprites[i].SetPosition(325, (i-5)*50 + 150);
        scoreText.SetPosition(425, (i-5)*50 + 150);
        scores.push_back(scoreText);
    }

    int timePlayed;
    file >> timePlayed;
    std::string time = to_string(timePlayed / 3600) + "h ";
    timePlayed = timePlayed % 3600;
    time.append(to_string(timePlayed / 60) + "m ");
    timePlayedText.SetString(time);
    timePlayedText.SetPosition(650, 310);
    timePlayedText.SetFont(mfont);
    sprites[12].SetPosition(525, 275);

    file >> score;
    sf::Text scoreText(score);
    scoreText.SetFont(mfont);
    sprites[11].SetPosition(525, 150);
    scoreText.SetPosition(650, 185);
    scores.push_back(scoreText);

}

void StatsState::checkButtons(float x, float y)
{
    backButton->checkHL(x, y);
}

void StatsState::clicked(float x, float y)
{
    if(backButton->isClicked(x, y))
        stateManager->popState();
}

void StatsState::handle()
{
    sf::Event Event;
    while (window->PollEvent(Event))
    {
        switch (Event.Type)
        {
            case sf::Event::Closed:
                window->Close();
                break;
            case sf::Event::MouseMoved:
                    checkButtons(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                break;
            case sf::Event::MouseButtonPressed:
                if(Event.MouseButton.Button == sf::Mouse::Left)
                {
                    clicked(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                    return;
                }
                break;
            default: break;
        }
    }
}

void StatsState::update()
{
    cursorSprite.SetPosition(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
}

void StatsState::draw()
{
    window->Clear(sf::Color(200, 255, 0));
    window->Draw(background);
    backButton->draw(window);
    for(int i = 0; i < scores.size(); i++)
        window->Draw(scores[i]);
    for(int i = 0; i < sprites.size(); i++)
        window->Draw(sprites[i]);
    window->Draw(timePlayedText);
    window->Draw(highscoreText);
    window->Draw(livesLostText);
    window->Draw(cursorSprite);
    window->Display();
}
